package com.charmzenus.it.common.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

public class StringUtil {


    /**
     * 随机盐
     *
     * @return
     */
    public static String getSalt(int length) {

        if (length < 8) length = 8;

        char[] chars = "0123456789abcdefghijklmnopqrwtuvzxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
        char[] saltchars = new char[length];
        Random RANDOM = new SecureRandom();
        for (int i = 0; i < length; i++) {
            int n = RANDOM.nextInt(62);
            saltchars[i] = chars[n];
        }
        return new String(saltchars);
    }

    /**
     *
     * @param plainText
     *            明文
     * @return 32位密文
     */
    public static String MD5(String plainText) {
        String re_md5 = new String();
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(plainText.getBytes());
            byte b[] = md.digest();

            int i;

            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }

            re_md5 = buf.toString();

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return re_md5;
    }

    /**
     * 时间戳
     * @return
     */
    public static int time() {
        return (int) (System.currentTimeMillis() / 1000);
    }


    //获得本月第一天0点时间   时间戳
    public static int getTimesMonthmorning(){
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0,0);
        cal.set(Calendar.DAY_OF_MONTH,cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        return (int) (cal.getTimeInMillis()/1000);
    }
    //获得本月最后一天24点时间     时间戳
    public static int getTimesMonthnight(){
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0,0);
        cal.set(Calendar.DAY_OF_MONTH,cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR_OF_DAY, 24);
        return (int) (cal.getTimeInMillis()/1000);
    }

    //获取当前时间一年后的时间戳
    public static int getOneYearLate(){
        Calendar cal = Calendar.getInstance();

        cal.set(cal.get(Calendar.YEAR)+1,cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0,0);

        return (int) (cal.getTimeInMillis()/1000);
    }


    //根据传入的 time 时间戳 续期 year 年
    public static int renewal(int time,int year){
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date((long) time*1000));
        cal.set(cal.get(Calendar.YEAR)+year,cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE),cal.get(Calendar.SECOND));
        return (int) (cal.getTimeInMillis()/1000);
    }

    //获取指定天数后的时间戳
    public static long pulsDay(int time,int day){
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date((long) time*1000));
        cal.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH)+day, cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE),cal.get(Calendar.SECOND));
        return (long) (cal.getTimeInMillis()/1000);
    }

    //获取指定分钟数后的时间戳
    public static int pulsMinute(int time,int minute){
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date((long) time*1000));
        cal.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE)+minute,cal.get(Calendar.SECOND));
        return (int) (cal.getTimeInMillis()/1000);
    }


    //将时间戳转换为日期字符串
    public static String UnixTimeSetDateTimeStr(Integer time){
        long newTime = new Long(time) * 1000;
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日  HH时mm分ss秒");//这个是你要转成后的时间的格式
        String sd = sdf.format(new Date(newTime));   // 时间戳转换成时间
        return sd;
    }

    //将时间戳转换为日期字符串
    public static String UnixTimeSetDateStr(Integer time){
        long newTime = new Long(time)*1000;
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");//这个是你要转成后的时间的格式
        String sd = sdf.format(new Date(newTime));   // 时间戳转换成时间
        return sd;
    }



}
